package edu.jmu.action.attendance;

import java.util.Date;
import java.util.LinkedHashMap;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

import edu.jmu.action.BaseAction;
import edu.jmu.bean.PageView;
import edu.jmu.bean.attendance.ManualSign;
import edu.jmu.bean.attendance.SignTag;
import edu.jmu.service.attendance.ManualSignService;
import edu.jmu.utils.WebUtil;

@Controller
@Scope(value = "prototype")
public class SignInManageAction extends BaseAction<ManualSign> {
	private static final long serialVersionUID = 1L;
	// private String id;

	@Resource
	private ManualSignService manualSignService;
	private ManualSign manualSign;
    private int userId;
    private String userName;
    public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getDapart() {
		return dapart;
	}

	public void setDapart(String dapart) {
		this.dapart = dapart;
	}

	public String getBanch() {
		return banch;
	}

	public void setBanch(String banch) {
		this.banch = banch;
	}

	public Date getText2() {
		return text2;
	}

	public void setText2(Date text2) {
		this.text2 = text2;
	}

	public String getTextarea() {
		return textarea;
	}

	public void setTextarea(String textarea) {
		this.textarea = textarea;
	}

	private String dapart;
    private String banch;
    private Date text2;
    private String textarea;
	@Validations(requiredStrings={
			
			@RequiredStringValidator(fieldName="user.description",message="签卡备注不能为空")
	},stringLengthFields={
			@StringLengthFieldValidator(fieldName="user.description",message="签卡备注长度过长，因小于100个字符",maxLength="100")
	})
	// @Permission(module="attendance",privilege="signIn",desc="展示签到信息")
	public String listSignIn() {
		pageView = new PageView<ManualSign>(maxresult, currentpage);
		java.util.LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		orderby.put("id", "desc");

		pageView.setQueryResult(manualSignService.getScrollData(
				pageView.getFirstResult(), pageView.getMaxresult(), orderby));

		return SUCCESS;
	}

	public String signIn() {
		SignTag signTag = SignTag.sign;
		Date date = new Date();
		@SuppressWarnings("deprecation")
		int hours = date.getHours();
		if (hours >= 9 && hours <= 10) {
			signTag = SignTag.later;
		} else if (hours > 10 && hours < 18) {
			signTag = SignTag.Absenteeism;
		}
		
		ManualSign entity = new ManualSign();
		entity.setSignTime(new Date());
		entity.setSignDesc(manualSign.getSignDesc());
		entity.setUser(WebUtil.getUser());
		entity.setSingTag(signTag);

		manualSignService.save(entity);
		return SUCCESS;
	}
    public String signOff() {
		SignTag signTag = SignTag.signOff;
		Date date = new Date();
		@SuppressWarnings("deprecation")
		int hours = date.getHours();
		if (hours <= 18 && hours >= 17) {
			signTag = SignTag.leave;
		} else if (hours < 17 && hours > 10 ) {
			signTag = SignTag.Absenteeism;
		}

		ManualSign entity = new ManualSign();
		entity.setSignTime(new Date());
		entity.setSignDesc(manualSign.getSignDesc());
		entity.setUser(WebUtil.getUser());
		entity.setSingTag(signTag);
		

		manualSignService.save(entity);
		return SUCCESS;
	}

	public ManualSign getManualSign() {
		return manualSign;
	}

	public void setManualSign(ManualSign manualSign) {
		this.manualSign = manualSign;
	}
	

}
